<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/2002/REC-xhtml1-20020801/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="nb-NO">
  <head>
    <title>Test suite for Validatious 2.0 standalone</title>
    <?php
        require '../utils/core.php';
        print scripts();
    ?>
    <script type="text/javascript">
// Opera needs this
function test() {
    assert(true);
}

function setUp() {
    v2.$('fs1').className = 'someClass';
}

function testV2ArrayEmptyArray() {
    var data = [];
    var arr = v2.array(data);
    assertNotUndefined(arr.push);
    assertEquals(0, arr.length);
}

function testV2ArrayWithArray() {
    var data = [1, 2, 'a'];
    var arr = v2.array(data);
    assertNotUndefined(arr.push);
    assertEquals(data.length, arr.length);
    assertEquals(data[0], arr[0]);
    assertEquals(data[1], arr[1]);
    assertEquals(data[2], arr[2]);
}

function testV2ArrayWithString() {
    var data = 'str';
    var arr = v2.array(data);
    assertNotUndefined(arr.push);
    assertEquals(1, arr.length);
    assertEquals(data, arr[0]);
}

function testV2ArrayWithNumber() {
    var data = 32;
    var arr = v2.array(data);
    assertNotUndefined(arr.push);
    assertEquals(1, arr.length);
    assertEquals(data, arr[0]);
}

function testV2ArrayWithNull() {
    var data = null;
    var arr = v2.array(data);
    assertNotUndefined(arr.push);
    assertEquals(0, arr.length);
}

function testV2ArrayWithUndefined(data) {
    var arr = v2.array(data);
    assertNotUndefined(arr.push);
    assertEquals(0, arr.length);
}

function testEmpty() {
    var undef = {};
    assertTrue(v2.empty(undef.prop));
    assertTrue(v2.empty(null));
    assertTrue(v2.empty(''));
    assertFalse(v2.empty('String'));
}

function testExtendEmptyObject() {
    var obj = {};
    v2.Object.extend(obj, { a: 1, b: 2 });
    assertNotNull(obj.a);
    assertNotNull(obj.b);

    var props = 0;
    for (p in obj) {
        props++;
    }

    assertEquals(2, props);
}

function testExtendObject() {
    var obj = { a: 1, b: 3 };
    v2.Object.extend(obj, { b: 4, c: 5 });
    assertEquals(4, obj.b);
    assertEquals(5, obj.c);

    var props = 0;
    for (p in obj) {
        props++;
    }

    assertEquals(3, props);
}

// Prototype has noe safe extend
function __testSafeExtendObject() {
}

function testArrayIndexOf() {
    var obj = { testProp: 'unit' };
    var arr = [1, 2, 3, 'a', false, obj, 2];
    assertEquals("function", typeof arr.indexOf);
    assertEquals(1, arr.indexOf(2));
    assertEquals(3, arr.indexOf('a'));
    assertEquals(4, arr.indexOf(false));
    assertEquals(5, arr.indexOf(obj));
}

function testGetWith$() {
    var div = v2.$('testDiv');
    assertNotUndefined(div.observe);
    assertNotUndefined(div.computedStyle);
    assertNotUndefined(div.visible);
    assertEquals('function', typeof div.visible);
}

function testGetWith$$() {
    var label = v2.$$('label[for=testEl]');
    assertNotNull(label[0]);
    assertEquals(document.getElementById('testElLabel'), label[0]);
    var nonExistent = v2.$$('label[for=someStuff]');
    assert('Non-existent label for some stuff exists', nonExistent === null || nonExistent.length === 0);

    var radios = v2.$$('input[type=radio][name=fruit]');
    assertEquals(3, radios.length);
    var el = document.getElementById('carrot');
    var flag = false;

    for (var i = 0; i < radios.length; i++) {
        if (radios[i] === el) {
            flag = true;
            break;
        }
    }

    assertFalse(flag);

    var checkboxes = v2.$$('input[type=checkbox].g_test');
    assertEquals(2, checkboxes.length);

    el = document.getElementById('pref3');

    for (var i = 0; i < checkboxes.length; i++) {
        if (checkboxes[i] === el) {
            flag = true;
            break;
        }
    }

    assertFalse(flag);

    var elements = v2.$$('input[name=testEl], select[name=single]');
    assertEquals(2, elements.length);
    assertEquals('testEl', elements[0].id);
    assertEquals('single', elements[1].id);
}

function testComputedStyle() {
    var div = v2.$('testDiv');
    div.style.background = 'red';
    var color = div.computedStyle('background-color');
    assertTrue('Div background color not red', color == 'rgb(255, 0, 0)' || color == 'red' || color == '#ff0000');
    div.style.height = '100px';
    assertEquals('Div height not 100px', '100px', div.computedStyle('height'));
}

function testVisible() {
    var div = v2.$('testDiv');
    assertTrue('Div not initially visible', div.visible());

    div.style.display = 'none';
    assertFalse('Div visible with display=none', div.visible());

    div.style.display = 'block';
    div.style.visibility = 'hidden';
    assertFalse('Div visible with visibility=hidden', div.visible());

    div.style.visibility = 'visible';
    div.parentNode.style.display = 'none';
    assertFalse('Div visible with body.display=none', div.visible());
}

function testHasClassName() {
    var fs1 = v2.$('fs1');
    assertTrue(v2.Element.hasClassName(fs1, 'someClass'));
    assertFalse(v2.Element.hasClassName(fs1, 'someOtherClass'));
    assertTrue(fs1.hasClassName('someClass'));
}

function testAddClassName() {
    var fs1 = v2.$('fs1');
    assertFalse(v2.Element.hasClassName(fs1, 'someOtherClass'));
    assertEquals(fs1, fs1.addClassName('someOtherClass'));
    assertTrue(fs1.hasClassName('someOtherClass'));
}

function testRemoveClassName() {
    var fs1 = v2.$('fs1');
    fs1.addClassName('someOtherClass');
    assertTrue(fs1.hasClassName('someOtherClass'));
    fs1.removeClassName('someOtherClass');
    assertFalse(fs1.hasClassName('someOtherClass'));
}

function testFunctionBind() {
    var obj = { testStr: 'Object test' };

    var fn = function() {
        return this.testStr;
    };

    assertNotUndefined(fn.bind);
    assertEquals('Object test', fn.bind(obj)());
    assertEquals('Another object test', fn.bind({ testStr: 'Another object test' })());
}

// Prototype has noe cache for bound functions
function __testFunctionBindCache() {
}
</script>
  </head>
  <body>
    <div id="testDiv"></div>
    <form>
      <fieldset id="fs1" class="someClass">
        <label for="testEl" id="testElLabel">Label 1</label>
        <input type="text" name="testEl" id="testEl" value="Input 1" />
        <label for="testEl2" id="testElLabel2">Label 2</label>
        <input type="text" name="testEl2" id="testEl2" value="Input 2" />
      </fieldset>
      <fieldset id="fs2">
        <input type="radio" name="fruit" value="apple" id="apple" />
        <label for="apple">Apple</label>
        <input type="radio" name="fruit" value="orange" id="orange" />
        <label for="orange">Orange</label>
        <input type="radio" name="fruit" value="banana" id="banana" checked="checked" />
        <label for="banana">Banana</label>
      </fieldset>
      <fieldset>
        <input type="radio" name="vegetable" value="carrot" id="carrot" class="g_test" />
        <label for="carrot">Carrot</label>
        <input type="radio" name="vegetable" value="cucumber" id="cucumber" class="g_test other" />
        <label for="cucumber">Cucumber</label>
      </fieldset>
      <fieldset>
        <input type="checkbox" name="somePref1" value="pref1" id="pref1" class="g_test" checked="checked" />
        <label for="pref1">Preference 1</label>
        <input type="checkbox" name="somePref2" value="pref2" id="pref2" class="g_test" />
        <label for="pref2">Preference 2</label>
        <input type="checkbox" name="somePref3" value="pref3" id="pref3" class="g_other_group" checked="checked" />
        <label for="pref3">Preference 3</label>
      </fieldset>
      <fieldset>
        <input type="checkbox" name="somePref4" value="pref4" id="pref4" class="g_test2" />
        <label for="pref4">Preference 4</label>
        <input type="checkbox" name="somePref5" value="pref5" id="pref5" class="g_test2" />
        <label for="pref5">Preference 5</label>
        <input type="checkbox" name="somePref6" value="pref6" id="pref6" class="g_test2" />
        <label for="pref6">Preference 6</label>
      </fieldset>
      <fieldset>
        <input type="hidden" name="hidden1" id="hidden1" value="Hidden!" />
        <input type="password" name="pass1" id="pass1" value="Password!" />
        <input type="submit" name="submit1" id="submit1" value="Submit me!" />
        <input type="button" name="button1" id="button1" value="A button!" />
      </fieldset>
      <fieldset>
        <select name="single" id="single">
          <option value="">Choose</option>
          <option value="1">First choice</option>
          <option selected="selected" value="2">Second choice</option>
          <option value="3">Third choice</option>
          <option value="4">Fourth choice</option>
          <option value="5">Fifth choice</option>
          <option value="6">Sixth choice</option>
          <option value="7">Seventh choice</option>
        </select>
      </fieldset>
      <fieldset>
        <select name="multi" id="multi" multiple="multiple">
          <option value="">Choose</option>
          <option value="1">First choice</option>
          <option value="2">Second choice</option>
          <option selected="selected" value="3">Third choice</option>
          <option value="4">Fourth choice</option>
          <option selected="selected" value="5">Fifth choice</option>
          <option value="6">Sixth choice</option>
          <option value="7">Seventh choice</option>
        </select>
      </fieldset>
      <fieldset>
        <textarea name="text" id="text">This is quite the novel</textarea>
      </fieldset>
    </form>
  </body>
</html>
